package com.kaka.bean;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;

import java.io.PrintWriter;

public class Result {
    private Integer code;//业务状态码  1-成功  0-失败
    private String message;//提示信息
    private Object data;//响应数据

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Result() {
    }

    public Result(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    //快速返回操作成功响应结果(带响应数据)
    public static void success(HttpServletRequest request, HttpServletResponse response, Object data) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.element("code", 200);
        jsonObject.element("message", "操作成功");
        jsonObject.element("data", data);
        try {
            PrintWriter writer = response.getWriter();
            writer.print(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void success(HttpServletRequest request, HttpServletResponse response, Object ...data) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.element("code", 200);
        jsonObject.element("message", "操作成功");
        jsonObject.element("data", data[0]);
        jsonObject.element("count", data[1]);
        try {
            PrintWriter writer = response.getWriter();
            writer.print(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //快速返回操作成功响应结果
    public static void success(HttpServletRequest request, HttpServletResponse response) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.element("code", 200);
        jsonObject.element("message", "操作成功");
        jsonObject.element("count", 0);
        jsonObject.element("data", 0);
        try {
            PrintWriter writer = response.getWriter();
            writer.print(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void error(HttpServletRequest request, HttpServletResponse response) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.element("code", 500);
        jsonObject.element("message", "操作失败");
        jsonObject.element("data", 0);
        try {
            PrintWriter writer = response.getWriter();
            writer.print(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
